version 18
drop _all

* change the root folder according to your computer directory
global root = "D:\WorkingPaper-Series\Township_Light\Replication_Files"  

global dofiles = "$root\dofile"     
global working_data = "$root\workdata"
global tables = "$root\table"
global figures = "$root\figure"

cd "$working_data"


******************
*****Pre Set******
******************

global geo_ctrl = "topography_t* slope_t* roughness_t*"
global wth_ctrl = "gaez_t* precipitation_t* sunlight_t*"
global dist_ctrl = "county_dist_t* dist_hz_t* dist_coast_t*"

global full_ctrl = "$geo_ctrl $wth_ctrl $dist_ctrl"

global geo_ctrl_1 = "topography_t_1 slope_t_1 roughness_t_1"
global wth_ctrl_1 = "gaez_t_1 precipitation_t_1 sunlight_t_1"
global dist_ctrl_1 = "county_dist_t_1 dist_hz_t_1 dist_coast_t_1"

global full_ctrl_1 = "$geo_ctrl_1 $wth_ctrl_1 $dist_ctrl_1"

global geo_ctrl_2 = "topography_t_2 slope_t_2 roughness_t_2"
global wth_ctrl_2 = "gaez_t_2 precipitation_t_2 sunlight_t_2"
global dist_ctrl_2 = "county_dist_t_2 dist_hz_t_2 dist_coast_t_2"

global full_ctrl_2 = "$geo_ctrl_2 $wth_ctrl_2 $dist_ctrl_2"

global matchvar = "county_dist topography slope gaez sunlight dist_hz dist_coast"


global pre_ctrl = "light_t road_t pop_t"

********************
***** Table C1 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC1.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"

reghdfe std_light village treat_trend, a(county_year idcode) cl(countycode)
qui sum std_light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' replace
reghdfe std_light village treat_trend $full_ctrl_1, a(county_year idcode) cl(countycode)
qui sum std_light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' append
reghdfe std_light village treat_trend $full_ctrl_1 $full_ctrl_2, a(county_year idcode) cl(countycode)
qui sum std_light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' append
reghdfe std_light village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum std_light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' append

**********IPW Weighting************
use "township_panel_final.dta", clear
logit treat $matchvar
predict pw
replace pw = 1-pw if treat==0
sum pw,d 
keep if pw > `r(p1)' & pw < `r(p99)'
gen ipw = 1/pw

reghdfe std_light village treat_trend $full_ctrl [pweight = ipw], a(county_year idcode) cl(countycode)
qui sum std_light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' append
***********************************

use "town_light_tbv.dta", clear

******** Coarsened Exact Matching ********
cem topography (#10) slope (#10) roughness (#10) gaez (#10) precipitation (#10) sunlight (#10) county_dist (#10) dist_hz (#10) dist_coast (#10) ,tr(treat)
reghdfe std_light village treat_trend $full_ctrl [aw = cem_weights] if cem_matched == 1, a(county_year idcode) cl(countycode)
qui sum std_light if cem_matched == 1
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' append


********************
***** Table C2 *****
********************

use "township_panel_final.dta", clear
mat mat_csdid = J(2,4,0)

reghdfe log_light treat_trend, a(idcode county_year) resid
predict light_resid, resid

csdid light_resid, i(idcode) t(year) g(first_year) agg(simple) cl(countycode)
mat mat_csdid[1,1] = e(b)[1,1]
mat mat_csdid[2,1] = sqrt(e(V)[1,1])
csdid light_resid $full_ctrl, reg i(idcode) t(year) g(first_year) agg(simple) cl(countycode)
mat mat_csdid[1,2] = e(b)[1,1]
mat mat_csdid[2,2] = sqrt(e(V)[1,1])
csdid light_resid $full_ctrl, i(idcode) t(year) g(first_year) agg(simple) cl(countycode)
mat mat_csdid[1,3] = e(b)[1,1]
mat mat_csdid[2,3] = sqrt(e(V)[1,1])
csdid light_resid $full_ctrl, drimp i(idcode) t(year) g(first_year) agg(simple) cl(countycode)
mat mat_csdid[1,4] = e(b)[1,1]
mat mat_csdid[2,4] = sqrt(e(V)[1,1])

svmat mat_csdid

keep if mat_csdid1 != .
keep mat_csdid*

export delimited using "$tables/tableC2.rtf", replace

********************
***** Table C3 *****
********************

mat define mat_sdid = J(2,4,0)

use "township_panel_final.dta",clear
reghdfe log_light treat_trend, a(county_year idcode) resid  
predict light_resid, resid
bys idcode: ipolate light_resid year, epolate gen(light_ipol)
bys idcode: drop if light_ipol==.
xtset idcode year
xtbalance, range(2010 2020)

sdid light_ipol idcode year village, vce(bootstrap) reps(500) seed(123)
mat mat_sdid[1,1] = e(b)[1,1]
mat mat_sdid[2,1] = sqrt(e(V)[1,1])

use "township_panel_final.dta",clear
reghdfe log_light treat_trend $full_ctrl_1, a(county_year idcode) resid  
predict light_resid, resid
bys idcode: ipolate light_resid year, epolate gen(light_ipol)
bys idcode: drop if light_ipol==.
xtset idcode year
xtbalance, range(2010 2020)

sdid light_ipol idcode year village, vce(bootstrap) reps(500) seed(123)
mat mat_sdid[1,2] = e(b)[1,1]
mat mat_sdid[2,2] = sqrt(e(V)[1,1])

use "township_panel_final.dta",clear
reghdfe log_light treat_trend $full_ctrl_1 $full_ctrl_2, a(county_year idcode) resid  
predict light_resid, resid
bys idcode: ipolate light_resid year, epolate gen(light_ipol)
bys idcode: drop if light_ipol==.
xtset idcode year
xtbalance, range(2010 2020)

sdid light_ipol idcode year village, vce(bootstrap) reps(500) seed(123)
mat mat_sdid[1,3] = e(b)[1,1]
mat mat_sdid[2,3] = sqrt(e(V)[1,1])

use "township_panel_final.dta",clear
reghdfe log_light treat_trend $full_ctrl, a(county_year idcode) resid  
predict light_resid, resid
bys idcode: ipolate light_resid year, epolate gen(light_ipol)
bys idcode: drop if light_ipol==.
xtset idcode year
xtbalance, range(2010 2020)

sdid light_ipol idcode year village, vce(bootstrap) reps(500) seed(123)
mat mat_sdid[1,4] = e(b)[1,1]
mat mat_sdid[2,4] = sqrt(e(V)[1,1])
svmat mat_sdid

keep if mat_sdid1 != .
keep mat_sdid*

export delimited using "$tables/tableC3.rtf", replace


********************
***** Table C4 *****
********************

use "village_panel_final.dta", clear

global outtable = "$tables/tableC4.rtf" 
local addstat = `"addstat("TBV", coef_all, "SE", se_all ,"TBV before 2016", coef_before_16, "SE before 2016",se_before_16, "TBV after 2016", coef_after_16, "SE after 2016",se_after_16)"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"

csdid log_light,notyet drimp i(idcode) t(year) g(first_year) agg(group) cl(countycode)
lincom GAverage
scalar coef_all = r(estimate)
scalar se_all =r(se)
lincom G2014+G2015
scalar coef_before_16 = r(estimate)
scalar se_before_16 =r(se)
lincom G2016+G2017+G2018+G2019
scalar coef_after_16 = r(estimate)
scalar se_after_16 =r(se)
outreg2 using $outtable, drop(_all) `addstat' `addtext' `outreg_opt' replace


csdid ihs_light,notyet drimp i(idcode) t(year) g(first_year) agg(group) cl(countycode)
lincom GAverage
scalar coef_all = r(estimate)
scalar se_all =r(se)
lincom G2014+G2015
scalar coef_before_16 = r(estimate)
scalar se_before_16 =r(se)
lincom G2016+G2017+G2018+G2019
scalar coef_after_16 = r(estimate)
scalar se_after_16 =r(se)
outreg2 using $outtable, drop(_all) `addstat' `addtext' `outreg_opt' append


********************
***** Table C5 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC5.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Town FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes, "Controls", Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd = "replace"

forvalues i = 10 (10) 50 {
	foreach w in log_light ihs_light {
		qui reghdfe `w' village treat_trend $full_ctrl if radius_`i'==1, a(county_year idcode) cl(countycode)
		qui sum `w' if radius_`i'==1
		outreg2 using $outtable, keep(village) `addstat' `addtext' `outreg_opt' `apd'
		local apd = "append"
	}
}

********************
***** Table C6 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC6.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Pseudo R-squared", e(r2_p))"'
local addtext = `"addtext("Town FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"

ppmlhdfe light village treat_trend, a(county_year idcode) vce(cl countycode)
qui sum light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' replace
ppmlhdfe light village treat_trend $full_ctrl_1, a(county_year idcode) vce(cl countycode)
qui sum light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' append
ppmlhdfe light village treat_trend $full_ctrl_1 $full_ctrl_2, a(county_year idcode) vce(cl countycode)
qui sum light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' append
ppmlhdfe light village treat_trend $full_ctrl, a(county_year idcode) vce(cl countycode)
qui sum light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' append

**********IPW Weighting************
logit treat county_dist topography slope gaez sunlight dist_hz dist_coast
predict pw
replace pw = 1-pw if treat==0
sum pw,d 
keep if pw > `r(p1)' & pw < `r(p99)'
gen ipw = 1/pw
***********************************

ppmlhdfe light village treat_trend $full_ctrl [pweight = ipw], a(county_year idcode) vce(cl countycode)
qui sum light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' append


********************
***** Table C7 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC7.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"


foreach w in e_commerce rural_industry {
	foreach y in log_light ihs_light {
		qui reghdfe `y' vil_`w'_mean treat_`w'_mean village $full_ctrl treat_trend, a(county_year idcode) cl(countycode)
		qui sum `y'
		outreg2 using $outtable, keep(village vil_`w'_mean)  `addstat' `addtext' `outreg_opt' `apd_opt'
		local apd_opt = "append"
	}
}

********************
***** Table C8 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC8.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "Year FE",Yes)"'
local outreg_opt = "nor nocons dec(3)"

reghdfe village digit_village rural_revital total_freq, a(idcode year) cl(citycode)
qui sum village
outreg2 using $outtable, keep(digit_village rural_revital total_freq)  `addstat' `addtext' `outreg_opt' replace
reghdfe N_village digit_village rural_revital total_freq, a(idcode year) cl(citycode)
qui sum N_village
outreg2 using $outtable, keep(digit_village rural_revital total_freq)  `addstat' `addtext' `outreg_opt' append
reghdfe village digit_village rural_revital total_freq if first_year<2016, a(idcode year) cl(citycode)
qui sum village
outreg2 using $outtable, keep(digit_village rural_revital total_freq)  `addstat' `addtext' `outreg_opt' append
reghdfe N_village digit_village rural_revital total_freq if first_year<2016, a(idcode year) cl(citycode)
qui sum N_village
outreg2 using $outtable, keep(digit_village rural_revital total_freq)  `addstat' `addtext' `outreg_opt' append
reghdfe village digit_village rural_revital total_freq if first_year>=2016|first_year==0, a(idcode year) cl(citycode)
qui sum village
outreg2 using $outtable, keep(digit_village rural_revital total_freq)  `addstat' `addtext' `outreg_opt' append
reghdfe N_village digit_village rural_revital total_freq if first_year>=2016|first_year==0, a(idcode year) cl(citycode)
qui sum N_village
outreg2 using $outtable, keep(digit_village rural_revital total_freq)  `addstat' `addtext' `outreg_opt' append

********************
***** Table C9 *****
********************

xtset idcode year
global outtable = "$tables/tableC9.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "Year FE",Yes)"'
local outreg_opt = "nor nocons dec(3)"

reghdfe village l.digit_village l.rural_revital l.total_freq, a(idcode year) cl(citycode)
qui sum village
outreg2 using $outtable, keep(l.digit_village l.rural_revital l.total_freq)  `addstat' `addtext' `outreg_opt' replace
reghdfe N_village l.digit_village l.rural_revital l.total_freq, a(idcode year) cl(citycode)
qui sum N_village
outreg2 using $outtable, keep(l.digit_village l.rural_revital l.total_freq)  `addstat' `addtext' `outreg_opt' append
reghdfe village l.digit_village l.rural_revital l.total_freq if first_year<2016, a(idcode year) cl(citycode)
qui sum village
outreg2 using $outtable, keep(l.digit_village l.rural_revital l.total_freq)  `addstat' `addtext' `outreg_opt' append
reghdfe N_village l.digit_village l.rural_revital l.total_freq if first_year<2016, a(idcode year) cl(citycode)
qui sum N_village
outreg2 using $outtable, keep(l.digit_village l.rural_revital l.total_freq)  `addstat' `addtext' `outreg_opt' append
reghdfe village l.digit_village l.rural_revital l.total_freq if first_year>=2016|first_year==0, a(idcode year) cl(citycode)
qui sum village
outreg2 using $outtable, keep(l.digit_village l.rural_revital l.total_freq)  `addstat' `addtext' `outreg_opt' append
reghdfe N_village l.digit_village l.rural_revital l.total_freq if first_year>=2016|first_year==0, a(idcode year) cl(citycode)
qui sum N_village
outreg2 using $outtable, keep(l.digit_village l.rural_revital l.total_freq)  `addstat' `addtext' `outreg_opt' append


*********************
***** Table C10 *****
*********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC10.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

qui su fiscal_pressure,d
reghdfe log_light vil_digit_village_mean village treat_digit_village_mean treat_trend $full_ctrl if fiscal_pressure<`r(p50)', a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village vil_digit_village_mean )  `addstat' `addtext' `outreg_opt' `apd_opt'
local apd_opt = "append"
qui su fiscal_pressure,d
reghdfe log_light vil_digit_village_mean village treat_digit_village_mean treat_trend $full_ctrl if fiscal_pressure>`r(p50)', a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village vil_digit_village_mean)  `addstat' `addtext' `outreg_opt' `apd_opt'
qui su fiscal_pressure,d
reghdfe log_light village_mention village treat_trend $full_ctrl if fiscal_pressure<`r(p50)', a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_mention village)  `addstat' `addtext' `outreg_opt' `apd_opt'
qui su fiscal_pressure,d
reghdfe log_light village_mention village treat_trend $full_ctrl if fiscal_pressure>`r(p50)', a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_mention village)  `addstat' `addtext' `outreg_opt' `apd_opt'
qui su fiscal_pressure,d
reghdfe log_light village_policy village village_treat treat_policy treat_trend $full_ctrl if fiscal_pressure<`r(p50)', a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_policy village)  `addstat' `addtext' `outreg_opt' `apd_opt'
qui su fiscal_pressure,d
reghdfe log_light village_policy village village_treat treat_policy treat_trend $full_ctrl if fiscal_pressure>`r(p50)', a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_policy village)  `addstat' `addtext' `outreg_opt' `apd_opt'

*********************
***** Table C11 *****
*********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC11.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"


reghdfe log_light village_cash_crop village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_cash_crop village)  `addstat' `addtext' `outreg_opt' replace
reghdfe ihs_light village_cash_crop village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village_cash_crop village)  `addstat' `addtext' `outreg_opt' append
reghdfe log_light village_food_crop village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_food_crop village)  `addstat' `addtext' `outreg_opt' append
reghdfe ihs_light village_food_crop village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village_food_crop village)  `addstat' `addtext' `outreg_opt' append


*********************
***** Table C12 *****
*********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC12.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

* Horserace
reghdfe log_light vil_digit_village_mean village_market village treat_digit_village_mean treat_trend $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_market vil_digit_village_mean village)  `addstat' `addtext' `outreg_opt' `apd_opt'
local apd_opt = "append"
reghdfe log_light village_mention village_market village treat_trend $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_market village_mention village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe log_light village_policy village_market village treat_policy village_treat treat_trend $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_market village_policy village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe log_light vil_digit_village_mean village_mention village_policy village_market village treat_digit_village_mean treat_policy village_treat treat_trend $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_market vil_digit_village_mean village_mention village_policy village)  `addstat' `addtext' `outreg_opt' `apd_opt'

*********************
***** Table C13 *****
*********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC13.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Pseudo R-squared", e(r2_p))"'
local addtext = `"addtext("Town FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

foreach cond in "(first_year>=0)" "(first_year>=2016|first_year==0)" "(first_year<2016)" {
	qui ppmlhdfe firm_entry village treat_trend if `cond', a(county_year idcode) vce(cl countycode)
	qui sum firm_entry
	outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
	qui ppmlhdfe firm_entry village treat_trend $full_ctrl if `cond', a(county_year idcode) vce(cl countycode)
	qui sum firm_entry
	outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
}

*********************
***** Table C14 *****
*********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC14.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

foreach cond in "(first_year>=0)" "(first_year>=2016|first_year==0)" "(first_year<2016)" {
	reghdfe log_firm_exit pre_village village treat_trend if `cond', a(county_year idcode) cl(countycode)
	qui sum log_firm_entry if `cond'
	outreg2 using $outtable, keep(pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
	reghdfe log_firm_exit pre_village village treat_trend $full_ctrl if `cond', a(county_year idcode) cl(countycode)
	qui sum log_firm_entry if `cond'
	outreg2 using $outtable, keep(pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
}

*********************
***** Table C15 *****
*********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC15.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

foreach cond in "(first_year>=0)" "(first_year>=2016|first_year==0)" "(first_year<2016)" {
	qui reghdfe log_firm_tbv pre_village village treat_trend if `cond', a(county_year idcode) cl(countycode)
	qui sum log_firm_tbv if `cond'
	outreg2 using $outtable, keep(pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
	qui reghdfe log_firm_tbv pre_village village treat_trend $full_ctrl if `cond', a(county_year idcode) cl(countycode)
	qui sum log_firm_tbv if `cond'
	outreg2 using $outtable, keep(pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
}


*********************
***** Table C16 *****
*********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC16.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

foreach y in road_ipol log_firm_entry {
	reghdfe `y' pre_vil_total_freq_mean vil_total_freq_mean pre_village village $full_ctrl treat_trend , a(county_year idcode) cl(countycode)
	qui sum `y'
	outreg2 using $outtable, keep(pre_vil_total_freq_mean vil_total_freq_mean pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
}


foreach w in CS_rotation_2014 {
	qui gen `w'_village = village*`w'
	qui gen `w'_pre_village = pre_village*`w'
	foreach y in road_ipol log_firm_entry {
		qui reghdfe `y' `w'_pre_village `w'_village pre_village village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
		qui sum `y' if `w'!=.
		outreg2 using $outtable, keep(`w'_pre_village `w'_village pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
		local apd_opt = "append"
	}
}

*********************
***** Table C17 *****
*********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableC17.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

reghdfe log_firm_entry village_road village treat_trend, a(county_year idcode) cl(countycode)
qui sum log_firm_entry
outreg2 using $outtable, keep(village_road village)  `addstat' `addtext' `outreg_opt' `apd_opt'
local apd_opt = "append"
reghdfe log_firm_entry village_road village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_firm_entry
outreg2 using $outtable, keep(village_road village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe log_firm_entry village_road village treat_trend if (first_year>=2016|first_year==0), a(county_year idcode) cl(countycode)
qui sum log_firm_entry
outreg2 using $outtable, keep(village_road village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe log_firm_entry village_road village treat_trend $full_ctrl if (first_year>=2016|first_year==0), a(county_year idcode) cl(countycode)
qui sum log_firm_entry
outreg2 using $outtable, keep(village_road village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe log_firm_entry village_road village treat_trend if (first_year<2016), a(county_year idcode) cl(countycode)
qui sum log_firm_entry
outreg2 using $outtable, keep(village_road village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe log_firm_entry village_road village treat_trend $full_ctrl if (first_year<2016), a(county_year idcode) cl(countycode)
qui sum log_firm_entry
outreg2 using $outtable, keep(village_road village)  `addstat' `addtext' `outreg_opt' `apd_opt'


********************
***** Table E1 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableE1.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Trend",Yes)"'
local addtext_alt = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Control for Fundamentals",Yes)"'

local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

reghdfe log_light pre_village village treat_trend $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' `apd_opt'
local apd_opt = "append"
reghdfe log_light pre_village village $pre_ctrl $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village)  `addstat' `addtext_alt' `outreg_opt' `apd_opt'
reghdfe ihs_light pre_village village treat_trend $full_ctrl , a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe ihs_light pre_village village $pre_ctrl $full_ctrl , a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village)  `addstat' `addtext_alt' `outreg_opt' `apd_opt'

********************
***** Table E2 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableE2.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local addtext_alt = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Control for Fundamentals",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

reghdfe log_light village treat_trend $full_ctrl if !inlist(period,-2,-3), a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' `apd_opt'
local apd_opt = "append"
reghdfe log_light village $pre_ctrl $full_ctrl if !inlist(period,-2,-3), a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village)  `addstat' `addtext_alt' `outreg_opt' `apd_opt'
reghdfe ihs_light village treat_trend $full_ctrl if !inlist(period,-2,-3), a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe ihs_light village $pre_ctrl $full_ctrl if !inlist(period,-2,-3), a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village)  `addstat' `addtext_alt' `outreg_opt' `apd_opt'

********************
***** Table E3 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableE3.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"


reghdfe log_light vil_digit_village_mean village treat_digit_village_mean $pre_ctrl $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(vil_digit_village_mean village)  `addstat' `addtext' `outreg_opt' `apd_opt'
local apd_opt = "append"
reghdfe ihs_light vil_digit_village_mean village treat_digit_village_mean $pre_ctrl $full_ctrl , a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(vil_digit_village_mean village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe log_light village_mention village $pre_ctrl $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_mention village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe ihs_light village_mention village $pre_ctrl $full_ctrl , a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village_mention village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe log_light village_policy village treat_policy village_treat $pre_ctrl $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_policy village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe ihs_light village_policy village treat_policy village_treat $pre_ctrl $full_ctrl , a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village_policy village)  `addstat' `addtext' `outreg_opt' `apd_opt'

********************
***** Table F1 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableF1.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"

reghdfe log_light village_poverty village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_poverty village)  `addstat' `addtext' `outreg_opt' replace
reghdfe log_light village_mcc village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_mcc village)  `addstat' `addtext' `outreg_opt' append
reghdfe log_light village_genelogy village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_genelogy village)  `addstat' `addtext' `outreg_opt' append
reghdfe log_light village_dist_88 village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_dist_88 village)  `addstat' `addtext' `outreg_opt' append
reghdfe log_light village_market village treat_trend $full_ctrl , a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_market village)  `addstat' `addtext' `outreg_opt' `apd_opt'

********************
***** Table F2 *****
********************

use "township_panel_final.dta", clear

global outtable = "$tables/tableF2.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

reghdfe log_pop village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_pop
outreg2 using $outtable, keep(village)  `addstat' `addtext' `outreg_opt' `apd_opt'
local apd_opt = "append"
reghdfe log_light village log_pop treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village log_pop)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe ihs_light village log_pop treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village log_pop)  `addstat' `addtext' `outreg_opt' `apd_opt'
